/*
**  File: Binom.c
*/

#include <stdlib.h>
#include <stdio.h>
#include "General.h"
#include "Binom.h"


/****************  globals  ****************/

static int   NFactMax = 0;
static long  *fact;

/* BEGIN_EXTH */

/**************** BinomialInit ****************/

void BinomialInit( void )
{
    FactorialInit();
}

/**************** Binomial ****************/

long Binomial( const int i, const int j )
{
    return( Factorial( i ) / Factorial( j ) / Factorial( i-j ) );
}

/**************** Factorial ****************/

long Factorial( const int i)
{
    if ( i < 0 || i > NFactMax )
    {
	    printf( "Factorial argument %i out of range\n", i );
	    return( 0L );
    }
    return( fact[i] );
}

/**************** FactorialInit ****************/

void FactorialInit( void )
{
	char  *fct_name = "FactorialInit";
    int  i;
    
	STATUS( "Initializing factorials" );
	
    NFactMax = 12;         /* largest value possible for long ints */

    fact = (long *) calloc( (size_t)(NFactMax+1), sizeof(long) );
    fact[0] = 1;
    for ( i=1; i<=NFactMax; i++ )
    {
	    fact[i] = fact[i-1] * i;
	    printf( "%li ", fact[i] );
    }
    putchar( '\n' );
}
